(function (vc) {
    vc.extends({
        data: {
            upShopHouseDetailInfo: {
                orderId: '',
                stateName: '',
                state: '',
                personName: '',
                createTime: '',
                personId: '',
                remark: '',
                orderType: '',
                inoutOrderProducts: []
            }
        },
        _initMethod: function () {
            let orderId = vc.getParam('orderId');
            if (!vc.notNull(orderId)) {
                vc.toast('非法操作');
                vc.getBack();
                return;
            }
            $that.upShopHouseDetailInfo.orderId = vc.getParam('orderId');
            $that._listOrder();
            $that._listInoutOrderProduct();
        },
        _initEvent: function () {
        },
        methods: {
            _listOrder: function () {
                var param = {
                    params: {
                        page: 1,
                        row: 1,
                        orderId: $that.upShopHouseDetailInfo.orderId
                    }
                };
                //发送get请求
                vc.http.apiGet('/inoutOrder/queryInoutOrder',
                    param,
                    function (json, res) {
                        var _storeOrderCart = JSON.parse(json);
                        vc.copyObject(_storeOrderCart.data[0], $that.upShopHouseDetailInfo);
                    }, function (errInfo, error) {
                        console.log('请求失败处理');
                    }
                );
            },
            _listInoutOrderProduct: function () {
                var param = {
                    params: {
                        page: 1,
                        row: 50,
                        orderId: $that.upShopHouseDetailInfo.orderId,
                        shopId: vc.getCurrentCommunity().shopId
                    }
                };
                //发送get请求
                vc.http.apiGet('/inoutOrder.queryInoutOrderProduct',
                    param,
                    function (json, res) {
                        var _storeOrderCartFeeManageInfo = JSON.parse(json);
                        $that.upShopHouseDetailInfo.inoutOrderProducts = _storeOrderCartFeeManageInfo.data;
                        $that.upShopHouseDetailInfo.inoutOrderProducts.forEach(item => {
                            item.actCount = item.prodCount;
                        });
                    }, function (errInfo, error) {
                        console.log('请求失败处理');
                    }
                );
            },
            doInoutOrder: function () {
                vc.http.apiPost(
                    '/inoutOrder/saveOrderIn',
                    JSON.stringify($that.upShopHouseDetailInfo),
                    {
                        emulateJSON: true
                    },
                    function (json, res) {
                        let _json = JSON.parse(json);
                        if (_json.code == 0) {
                            vc.toast("入库成功");
                            vc.goBack();
                            return;
                        }
                    },
                    function (errInfo, error) {
                        console.log('请求失败处理');
                        vc.toast(errInfo);
                    });
            },
            _doCancelOrder: function () {
                $that.upShopHouseDetailInfo.state = "4004";
                vc.http.apiPost(
                    '/inoutOrder/updateInoutOrder',
                    JSON.stringify($that.upShopHouseDetailInfo),
                    {
                        emulateJSON: true
                    },
                    function (json, res) {
                        let _json = JSON.parse(json);
                        if (_json.code == 0) {
                            vc.toast(_json.msg);
                            var _cancelProduct = {};
                            _cancelProduct.orderId = $that.upShopHouseDetailInfo.orderId;
                            _cancelProduct.statusCd = "0";
                            _cancelProduct.state = "4004";
                            $that.upShopHouseDetailInfo.inoutOrderProducts.forEach(item => {
                                item.state = "4004";
                            });
                            $that.upShopHouseDetailInfo.stateName = "入库失败";
                            vc._doCancelProduct(_cancelProduct);
                            return;
                        }
                        vc.toast(_json.msg);
                    },
                    function (errInfo, error) {
                        console.log('请求失败处理');
                        vc.toast(errInfo);
                    });
            },
            _doCancelProduct: function (_inoutProduct) {
                _inoutProduct.state = "4004";
                console.log(_inoutProduct);
                vc.http.apiPost(
                    '/inoutOrderProduct/updateInoutOrderProduct',
                    JSON.stringify(_inoutProduct),
                    {
                        emulateJSON: true
                    },
                    function (json, res) {
                        let _json = JSON.parse(json);
                        if (_json.code == 0) {
                            vc.toast(_json.msg);
                            return;
                        }
                        vc.toast(_json.msg);
                    },
                    function (errInfo, error) {
                        console.log('请求失败处理');
                        vc.toast(errInfo);
                    });
            },
            _goBack: function () {
                vc.goBack()
            }
        }
    });
})(window.vc);
